678. 有效的括号字符串
为保证权益,题目请参考 678. 有效的括号字符串(From LeetCode).
解决方案1
Python
python
# 678. 有效的括号字符串
# https://leetcode-cn.com/problems/valid-parenthesis-string/
################################################################################
class Solution:
def checkValidString(self, s: str) -> bool:
bracket_high_level = 0
bracket_low_level = 0
for c in s:
if c == "(":
bracket_high_level += 1
bracket_low_level += 1
elif c == ")":
bracket_high_level -= 1
bracket_low_level -= 1
elif c == "*":
bracket_high_level += 1
bracket_low_level -= 1
if bracket_high_level < 0:
return False
if bracket_low_level < 0:
bracket_low_level = 0
if bracket_low_level != 0:
return False
return True
################################################################################
if __name__ == "__main__":
solution = Solution()
print(
solution.checkValidString(
# "*((*((**(((*)*****((*("
# "*("
# "(*)"
"**)))"
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47